<template>
    <div class="fromEdit"  v-loading="pageLoading">
        <div class="addbtn">
            <el-button type="primary" @click="addFrom"><Icon type="md-add" />新增表单字段</el-button>
            <el-button type="primary" @click="commitFrom"> 提 交 </el-button>
            <el-button type="" @click="$parent.showList()"> 返 回 </el-button>
        </div>

        <div>
            <div style="display:flex;flex-direction:row;padding:10px 0;">
                <label style="width:120px;font-weight:normal;text-align:right;padding-top:4px;">SQL查询语句：</label>
                <el-input v-model="sql" clearable placeholder="请输入内容"></el-input>
            </div>
            <el-collapse v-model="activeNames">
                <el-collapse-item v-for="(item,index) in fromList" :key="index+1" :title="'第('+(index+1)+')条'" :name="index+1">
                    <div>
                        <label for="">字段名称：</label>
                        <el-input v-model="item.filedName" clearable placeholder="请输入内容"></el-input></div>
                    <div>
                        <label for="">中文名称：</label>
                        <el-input v-model="item.filedCnName" clearable placeholder="请输入内容"></el-input></div>
                    <div style="padding-left: 20px;width: 10%;">
                        <el-popconfirm :title="'确定删除第'+(index+1)+'条数据吗？'" icon-color="#f00" @confirm="deleteRowData(index)">
                            <el-button slot="reference" type="danger" size="mini" ><i style="font-size: 16px;" class="el-icon-delete"></i></el-button>
                        </el-popconfirm>
                    </div>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
  import request from '@/utils/request'
export default{
    props:{
        getSelectObj:{
            type:Object,
            default:()=>{
                return{}
            },
            require:true
        }
    },
    data(){
        return {   
            pageLoading:false, 
            pageObj:null,
            label1:true,
            label2:false,
            sql:"",
            fromList:[],
            obj:{
                "filedName": "",
                "key": "",
                "filedCnName": "",
            },
            activeNames:''
        }
    },
    mounted(){
        this.$nextTick(res=>{
            let parObj = JSON.parse(this.getSelectObj)
            this.pageObj = parObj.paramConfig? JSON.parse(parObj.paramConfig):{}
            this.sql = this.pageObj.selectSql
            if(this.pageObj.configs){
                this.fromList = this.pageObj.configs?this.pageObj.configs:[]
            }

            
            if(this.fromList.length!=0){
                this.activeNames = []
                this.fromList.forEach((item,index)=>{
                    this.activeNames.push(index+1)
                })
            }
        })
    },   
    methods:{
        commitFrom(){
            let count = -1;
            for(let i=0;i<this.fromList.length;i++){
                let item = this.fromList[i]
                if(!item.filedName||!item.filedCnName){
                    count = i
                    break
                }
            }
            if(count!=-1){
                this.$message.error(`请把第${count+1}条数据填写完整`)
                return 
            }else{
                this.commitjk(this.fromList)
            }
        },
        commitjk(v){

            let p ={
                "paramConfig": JSON.stringify({
                    selectSql:this.sql,
                    configs:this.fromList
                }),
                "id":JSON.parse(this.getSelectObj).id
            }
            this.pageLoading = true
            request({
                url: '/ht-bpmn/ht-bpmn/complete/saveOrUpdate',
                method: 'post',
                data:p
            }).then(res => {
                this.pageLoading = false
                if (res.code == 200) {
                    this.$parent.showList('true')
                    this.fromList = []
                    this.$message.success(res.msg)
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        deleteRowData(index){
            const a = JSON.parse(JSON.stringify(this.fromList))
            a.splice(index, 1)
            this.fromList = JSON.parse(JSON.stringify(a))
        },
        reduceListSelectFun(index,chIndex){
            const a = JSON.parse(JSON.stringify(this.fromList[index].options))
            a.splice(chIndex, 1)
            this.fromList[index].options = JSON.parse(JSON.stringify(a))
        },
        reduceListFun(index,chIndex) {
            const a = JSON.parse(JSON.stringify(this.fromList[index].requestBody))
            a.splice(chIndex, 1)
            this.fromList[index].requestBody = JSON.parse(JSON.stringify(a))
        },
        addSelectrequestBody(index){
            let a = JSON.parse(JSON.stringify(this.fromList))
            a[index].children.options.push({
                "optionName": "",
                "optionValue": ""
            })
            this.fromList = JSON.parse(JSON.stringify(a))
        },
        addrequestBody(index){
            let a = JSON.parse(JSON.stringify(this.fromList))
            a[index].requestBody.push({
                "filedName": "",
                "filedCnName": "",
                "required": true
            })
            this.fromList = JSON.parse(JSON.stringify(a))
        },
        addListFun(){

        },
        addFrom(){
            this.fromList.push({...this.obj})
            this.activeNames = this.fromList.length
        },
        deleteOutFrom(){

        },
        deleteInnerFrom(){

        },
        changeTypeData(type,index){
            if(type=='api'){
                this.fromList[index].requestBody =[]
            }
            if(type=='select'){
                if(!this.fromList[index].hasOwnProperty('children')){
                    this.$set(this.fromList[index],'children',{key:"",options:[]})
                }else{
                    this.fromList[index].children.options =[]
                }
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.fromEdit{
    position: relative;
    .addbtn{
        // position: fixed;
        background: #fff;
        width: 100%;
        z-index: 999;
        padding:0 0 10px 0;
    }
}
.parDel{
    color: red;
    line-height: 32px;
    margin-left: 10px;
    font-size: 18px;
    cursor: pointer;
}
>>>.el-collapse-item__wrap{
    padding-top: 20px;
}
>>>.el-collapse-item__header{
    background: #f2f5f8;
    padding-left: 10px;
}
>>>label.el-radio {
    width: 45px !important;
    display: inline-block;
    margin-right: 10px;
}
.requestBodyClass{
    .requestBodyContent{
        >div{
            display: flex;
            flex-direction: row;
            margin-bottom: 10px;
            p{
                display: flex;
                flex-direction: row;
                width: 20%;
            }
        }
    }
}
>>>.el-collapse-item__content {
    padding-bottom: 25px;
    font-size: 13px;
    color: #303133;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    >div{
        display: flex;
        flex-direction: row;
        width: 20%;
        margin-bottom: 10px;
        label{
            display: inline-block;
            width: 110px;
            text-align: right;
            line-height: 32px;
            font-size: 12px;
            font-weight: normal;
        }
    }
}
</style>